<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          v-hasPermi="['production:workRecord:query']"
        >
          搜索
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['production:workRecord:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['production:workRecord:edit']"
        >
          修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="single"
          @click="handleDelete"
          v-hasPermi="['production:workRecord:del']"
        >
          删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="(tjFalg = single || mxDate)"
          @click="handleSubmit"
          v-hasPermi="['production:workRecord:post']"
        >
          提交
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="small"
          :loading="exportLoading"
          @click="handleDc('#exportPutStorage', '来料入库单据')"
          v-hasPermi="['production:workRecord:export']"
        >
          Excel
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!--头表Tab-->
    <el-table
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :height="tableH"
      :data="YzmMstrList"
      @row-click="clickRwm"
      id="exportPutStorage"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" type="index" width="40" align="center" />
      <el-table-column
        label="运转记录编号"
        :show-overflow-tooltip="true"
        align="center"
        prop="yzmDjbh"
      />
      <el-table-column
        label="调整日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="yzmTzrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.yzmTzrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="班次"
        :show-overflow-tooltip="true"
        align="center"
        prop="yzmBbdm"
      >
        <template #default="scope">
          <dict-tag :options="BbdmOptions" :value="scope.row.yzmBbdm" />
        </template>
      </el-table-column>
      <el-table-column
        label="建立人员"
        :show-overflow-tooltip="true"
        align="center"
        prop="yzmJlry"
      />
      <el-table-column
        label="建立日期"
        :show-overflow-tooltip="true"
        align="center"
        prop="yzmJlrq"
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.yzmJlrq, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        :show-overflow-tooltip="true"
        align="center"
        prop="yzmDesc"
      />
      <el-table-column
        label="操作"
        align="center"
        width="80"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            @click="serachYzdData(scope.row)"
            v-hasPermi="['production:workRecord:addChildren']"
            >添加</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!--子表Tab-->
    <el-table
      highlight-current-row
      :data="yzdDetList"
      border
      stripe
      :height="tableHChild"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column
        label="#"
        type="index"
        width="55"
        align="center"
        fixed="left"
      />
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="itmCustwldm"
        fixed="left"
      />
      <el-table-column
        label="物料代码"
        :show-overflow-tooltip="true"
        width="135"
        align="center"
        prop="moeWldm"
      />
      <el-table-column
        label="物料品名"
        :show-overflow-tooltip="true"
        width="320"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column
        label="机台计数"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdJtsl"
      />
      <el-table-column
        label="调整数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdSjsl"
      />
      <el-table-column
        label="良品数调整差异"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdLpcy"
      />
      <el-table-column
        label="操作人员1"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp1"
      />
      <el-table-column
        label="操作人员2"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp2"
      />
      <el-table-column
        label="操作人员3"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp3"
      />
      <el-table-column
        label="操作人员4"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp4"
      />
      <el-table-column
        label="操作人员5"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp5"
      />
      <el-table-column
        label="操作人员6"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp6"
      />
      <el-table-column
        label="操作人员7"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp7"
      />
      <el-table-column
        label="操作人员8"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="yzdOp8"
      />

      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        width="80"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleYzdDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加子项对话框   -->
    <el-dialog
      v-model="openChildren"
      :fullscreen="true"
      :close-on-click-modal="false"
      append-to-body
    >
      <!-- 子项搜索条件框-->
      <el-form :model="queryChildParams" :inline="true">
        <el-row>
          <el-col :span="18">
            <el-form-item label="机台编号" prop="yzmDesc">
              <el-select
                v-model="queryChildParams.yzmDesc"
                filterable
                placeholder="请选择机台编号"
                clearable
              >
                <el-option
                  v-for="item in JtmOptions"
                  :key="item.jtmJtbh"
                  :label="item.jtmJtbh"
                  :value="item.jtmJtbh"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="综合查询" prop="itmPmgg">
              <el-input
                v-model="queryChildParams.itmPmgg"
                placeholder="请输入客户代码或品名规格"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item style="display: flex; justify-content: flex-end">
              <el-button type="primary" @click="serachChildrened"
                >查询</el-button
              >
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <!--子项数据框-->
      <div class="work">
        <el-table
          highlight-current-row
          v-loading="loading"
          :data="serachAddList"
          border
          stripe
          height="calc(50% + 20px)"
          ref="somCheckTable"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            min-width="5%"
            type="index"
            align="center"
          />
          <el-table-column
            label="机台编号"
            width="75"
            :show-overflow-tooltip="true"
            align="center"
            prop="yzdJtbh"
          />
          <el-table-column
            label="客户编号"
            width="120"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmCustwldm"
          />
          <el-table-column
            label="产品名称"
            width="200"
            :show-overflow-tooltip="true"
            prop="itmPmgg"
          />
          <el-table-column
            label="物料代码"
            width="135"
            :show-overflow-tooltip="true"
            align="center"
            prop="moeWldm"
          />
          <el-table-column
            label="机台取数"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="yzdJtsl"
          />
          <el-table-column
            label="实际产出"
            width="100"
            :show-overflow-tooltip="true"
            prop="yzdSjsl"
            align="center"
          >
            <template #default="scope">
              <el-input
                type="number"
                class="inputName"
                v-model="scope.row.yzdSjsl"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作工1" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp1"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工2" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp2"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工3" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp3"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工4" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp4"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工5" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp5"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工6" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp6"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工7" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp7"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工8" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp8"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="130"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="serachYzdDataChild(scope.row)"
                v-hasPermi="['production:workRecord:addChildren']"
              >
                添加
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-table
          highlight-current-row
          v-loading="loading"
          :data="serachAddListChild"
          border
          stripe
          height="calc(50% - 120px)"
          ref="somCheckTable"
          @scrollBottom="load2"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            min-width="5%"
            type="index"
            align="center"
          />
          <el-table-column
            label="机台编号"
            width="75"
            :show-overflow-tooltip="true"
            align="center"
            prop="yzdJtbh"
          />
          <el-table-column label="客户编号" width="120" align="center">
            <template #default="scope">
              <el-autocomplete
                @change="leaveInput(scope.$index)"
                v-model="scope.row.itmCustwldm"
                value-key="itm_custwldm"
                :fetch-suggestions="querySearch"
                placeholder="输入可筛选"
              ></el-autocomplete>
              <!-- <el-select
                @change="leaveInput(scope.$index)"
                v-model="scope.row.itmCustwldm"
                style="width: 100px"
                filterable
                placeholder="输入可筛选"
                class="inputNameSend">
                <el-option v-for="dict in cusWldmOptions" :key="dict.itm_custwldm" :value="dict.itm_custwldm"></el-option>
              </el-select> -->
            </template>
          </el-table-column>
          <el-table-column
            label="产品名称"
            width="180"
            :show-overflow-tooltip="true"
            align="center"
            prop="itmPmgg"
          />
          <el-table-column
            label="物料代码"
            width="120"
            :show-overflow-tooltip="true"
            align="center"
            prop="moeWldm"
          />
          <el-table-column
            label="机台取数"
            width="100"
            :show-overflow-tooltip="true"
            align="center"
            prop="yzdJtsl"
          />
          <el-table-column
            label="实际产出"
            width="100"
            :show-overflow-tooltip="true"
            prop="yzdSjsl"
            align="center"
          >
            <template #default="scope">
              <el-input
                type="number"
                class="inputName"
                v-model="scope.row.yzdSjsl"
              ></el-input>
            </template>
          </el-table-column>
          <el-table-column label="操作工1" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp1"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工2" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp2"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工3" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp3"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工4" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp4"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工5" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp5"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工6" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp6"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工7" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp7"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column label="操作工8" width="130" align="center">
            <template #default="scope">
              <el-autocomplete
                v-model="scope.row.yzdOp8"
                value-key="nickName"
                :fetch-suggestions="querySearchUser"
                placeholder="选择人员"
              ></el-autocomplete>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="130"
            align="center"
            fixed="right"
          >
            <template #default="scope">
              <el-button
                size="mini"
                type="text"
                @click="
                  () => {
                    //console.log(scope)
                    serachAddListChild.splice(scope.index, 1);
                  }
                "
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="margin-top: 25px; display: flex; justify-content: flex-end">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="saveYzdData">确认</el-button>
        </div>
      </div>
    </el-dialog>
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="430px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="108px"
      >
        <el-col :span="24">
          <el-form-item label="运转记录编号" prop="yzmDjbh">
            <el-input
              v-model="queryParams.yzmDjbh"
              style="width: 240px"
              placeholder="请输入运转记录编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调整日期">
            <el-date-picker
              v-model="dateRange"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              style="width: 240px"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="调整班次" prop="yzmBbdm">
            <el-select
              style="width: 240px"
              v-model="queryParams.yzmBbdm"
              filterable
              placeholder="请选择班次"
              clearable
            >
              <el-option
                v-for="item in BbdmOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改运转记录对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="480px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="108px">
        <el-col :span="24">
          <el-form-item label="运转记录编号" prop="yzmDjbh">
            <el-input
              v-model="form.yzmDjbh"
              placeholder="流水号生成"
              disabled
            />
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="调整日期" prop="yzmTzrq">
            <el-date-picker
              clearable
              size="small"
              v-model="form.yzmTzrq"
              type="date"
              style="width: 332px"
              value-format="yyyy-MM-dd"
              placeholder="选择调整日期"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="调整班次" prop="yzmBbdm">
            <el-select
              v-model="form.yzmBbdm"
              style="width: 332px"
              filterable
              placeholder="请选择班次"
              clearable
            >
              <el-option
                v-for="item in BbdmOptions"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="备注信息" prop="yzmDesc">
            <el-input v-model="form.yzmDesc" placeholder="请输入备注信息" />
          </el-form-item>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { exportTable } from "@/utils/ruoyi";
import {
  listProduction,
  getRecord,
  addRecord,
  updateRecord,
  delRecord,
  getYzdlist,
  delYzd,
  searchData,
  saveDate,
  submit,
} from "@/api/production/workRecord";
import { userDict } from "@/api/system/user";
import { jtmDict } from "@/api/project/jtm";
import { cusWldmDict, getItmMstrByCustwldm } from "@/api/project/materiel";
let cusWldmOptions = [];
let UserOptions = [];
export default {
  name: "WorkRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      serachChildren: false,
      // 选中数组
      ids: [],
      serachAddListChild: [],
      // 非单个禁用
      tableH: (document.body.clientHeight - 130) / 2,
      tableHData: (document.body.clientHeight - 270) / 2,
      tableHDataChild: 60,
      tableHChild: (document.body.clientHeight - 280) / 2,
      // 非多个禁用
      multiple: true,
      yzdDetList: [],
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      openChildren: false,
      // 总条数
      total: 0,
      total2: 0,
      // 运转记录头表数据
      YzmMstrList: [],
      title: "",
      addDate: true,
      // 是否显示弹出层
      open: false,
      mxDate: true,
      tjFalg: false,
      rkmxList: [],
      rktjList: [],
      qgmxList: [],
      rowData: null,
      qgdDetCountList: [],
      serachAddList: [],
      unitFlagOptions: [],
      BbdmOptions: [],
      UserOptions: [],
      JtmOptions: [],
      selectioned: [],
      single: true,
      dateRange: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        yzmDjbh: null,
        yzmTzrq: new Date(),
        yzmBbdm: null,
        pageNum: 1,
        pageSize: document.body.clientHeight > 900 ? 30 : 20,
      },
      // 查询子项参数
      queryChildParams: {
        pageNum: 1,
        pageSize: 25,
        yzmTzrq: null,
        yzmBbdm: null,
        yzmDjbh: null,
        //用作机台
        yzmDesc: null,
        itmPmgg: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        yzmTzrq: [
          { required: true, message: "调整日期不能为空", trigger: "blur" },
        ],
        yzmBbdm: [{ required: true, message: "班别不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getList();
    //班次代码
    this.getDicts("class_code").then((response) => {
      this.BbdmOptions = response.data;
    });
    //用户字典
    userDict().then((res) => {
      UserOptions = res.data;
    });
    jtmDict("0").then((res) => {
      this.JtmOptions = res.data;
    });
    cusWldmDict().then((res) => {
      cusWldmOptions = res.data;
    });
  },
  methods: {
    querySearch(queryString, cb) {
      var results = queryString
        ? cusWldmOptions.filter((a) => {
            return a.itm_custwldm.indexOf(queryString) > -1;
          })
        : cusWldmOptions;
      // 调用 callback 返回建议列表的数据
      cb(results.slice(0, 20));
    },
    querySearchUser(queryString, cb) {
      queryString = (queryString || "").toLowerCase();
      let results = queryString
        ? UserOptions.filter((a) => {
            return (
              a.nickName.toLowerCase().indexOf(queryString) > -1 ||
              a.userName.toLowerCase().indexOf(queryString) > -1
            );
          })
        : UserOptions;
      // 调用 callback 返回建议列表的数据
      cb(results.slice(0, 20));
    },
    /** 查询来料进货入库开单列表 */
    getList(row) {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.dateRange && "" != this.dateRange) {
        this.queryParams.params["beginDate"] = this.moment(
          this.dateRange[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endDate"] = this.moment(
          this.dateRange[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      listProduction(this.queryParams).then((response) => {
        this.YzmMstrList = [];
        this.YzmMstrList = this.YzmMstrList.concat(response.rows);
        this.loading = false;
      });
      if (row != null) {
        this.clickRwm(row);
      } else if (this.YzmMstrList != null && this.YzmMstrList.length > 0) {
        this.clickRwm(this.YzmMstrList[0]);
      }
    },
    clickRwm(row) {
      //获得选中的详细数据
      this.rowData = row;
      //获得当前单据的状态
      this.ids = row.yzmId;
      this.single = !(row != null && row.yzmZtbz == "1");
      //获得选中的详细数据
      this.getyzdDetList(row.yzmDjbh);
    },
    /** 获取YzdDet 数据*/
    getyzdDetList(yzmDjbh) {
      this.yzdDetList = [];
      if (yzmDjbh != null && yzmDjbh != "") {
        getYzdlist(yzmDjbh).then((response) => {
          this.yzdDetList = response.data;
          this.mxDate = this.yzdDetList.length > 0 ? false : true;
        });
      }
    },
    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        listProduction(this.queryParams).then((response) => {
          this.YzmMstrList = this.YzmMstrList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
          if (row != null) {
            this.clickRwm(row);
          } else if (this.YzmMstrList != null && this.YzmMstrList.length > 0) {
            this.clickRwm(this.YzmMstrList[0]);
          }
        });
      }
      return 0;
    },
    //根据客户编号查询物料信息
    leaveInput(index) {
      const itmCustwldm = this.serachAddListChild[index].itmCustwldm;
      if (itmCustwldm != null && itmCustwldm.trim() !== "") {
        getItmMstrByCustwldm(itmCustwldm).then((response) => {
          const data = response.data;
          this.serachAddListChild[index].moeWldm = data[0].itmWldm;
          this.serachAddListChild[index].itmPmgg = data[0].itmPmgg;
        });
      }
    },
    /** 查询子项 */
    serachChildrened() {
      this.queryChildParams.pageNum = 1;
      searchData(this.queryChildParams).then((response) => {
        this.serachAddList = [];
        this.serachAddList = this.serachAddList.concat(response.rows);
        this.total2 = response.total;
      });
    },
    load2() {
      if (
        this.queryChildParams.pageNum <
        Math.ceil(this.total2 / this.queryChildParams.pageSize)
      ) {
        this.queryChildParams.pageNum++;
        searchData(this.queryChildParams).then((response) => {
          this.serachAddList = this.serachAddList.concat(response.rows);
          this.total2 = response.total;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.openChildren = false;
      this.serachAddListChild = [];
      this.reset();
    },
    handleSelectionChangeC(selection) {
      this.selectioned = selection;
    },
    //添加-确定
    saveYzdData() {
      let data = this.serachAddList
        .concat(this.serachAddListChild)
        .filter((a) => {
          return !!a.yzdSjsl && !!a.itmCustwldm;
        });
      if (data.some((o) => o.yzdJtsl - o.yzdSjsl > o.yzdRksl)) {
        this.msgError("良品数调整数量不能低于入库数!");
        return;
      }
      if (data.length < 1) {
        this.msgError("实际数量必须输入!");
      } else {
        saveDate(data).then((res) => {
          this.serachChildrened();
          this.selectioned = [];
          this.msgSuccess("添加成功");
        });
      }
    },

    serachYzdData(row) {
      this.openChildren = true;
      this.queryChildParams.yzmTzrq = row.yzmTzrq;
      this.queryChildParams.yzmBbdm = row.yzmBbdm;
      this.queryChildParams.yzmDjbh = row.yzmDjbh;
      this.serachChildrened();
    },
    serachYzdDataChild(row) {
      let objChild = {
        yzdJtbh: row.yzdJtbh,
        yzdJtsl: 0,
        yzdDjbh: row.yzdDjbh,
      };
      this.serachAddListChild.push(objChild);
    },
    handleYzdDelete(row) {
      delYzd(row.yzdId)
        .then((res) => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {
          this.msgError("明细删除失败");
        });
    },
    // 表单重置
    reset() {
      this.form = {
        yzmId: null,
        yzmDjbh: null,
        yzmTzrq: new Date(),
        yzmBbdm: null,
        yzmJlry: null,
        yzmJlrq: null,
        yzmDesc: null,
        yzmZtbz: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.YzmMstrList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeRwmJlry = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运转记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const yzmId = row.yzmId || this.ids;
      getRecord(yzmId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运转记录";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.yzmId != null) {
            updateRecord(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete() {
      const yzmDjbh = this.rowData.yzmDjbh;
      this.$confirm(
        '是否确认删除来料进货入库开单编号为"' + yzmDjbh + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          return delRecord(yzmDjbh);
        })
        .then(() => {
          this.YzmMstrList = [];
          this.queryParams.pageNum = 1;
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    handleSubmit() {
      submit(this.rowData.yzmId).then((res) => {
        this.msgSuccess("提交成功");
        this.getList();
      });
    },
    handleDc(a, b) {
      exportTable(a, b);
    },
  },
};
</script>

<style lang="scss" scoped>
.work {
  height: 100%;
  ::v-deep .el-input > .el-input__inner {
    height: 22px;
    line-height: 22px;
  }

  .inputName {
    ::v-deep .el-input .el-input__inner {
      height: 23px;
      line-height: 23px;
    }
  }
}
</style>
